package com.example.guideapp.utils;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.guideapp.bean.Festival;
import com.example.guideapp.bean.FestivalMessage;
import com.example.guideapp.bean.MyFestival;
import com.example.guideapp.bean.WishType;
import com.example.guideapp.bean.WishTypeMessage;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/20.
 */

public class DatabaseManage {

    private static MySqliteDatabaseHelper helper;

    public static synchronized MySqliteDatabaseHelper getInstance(Context context){
        if (helper == null){
            helper = new MySqliteDatabaseHelper(context);
        }
        return helper;
    }

    public static void createDatabase(){
        SQLiteDatabase db = helper.getReadableDatabase();
    }


    public static List<Festival> festivalCursorToList(Cursor cursor){
        List<Festival> list = new ArrayList<>();
        Festival festival = null;
        while (cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex(Constant.FESTIVAL_ID));
            String name = cursor.getString(cursor.getColumnIndex(Constant.FESTIVAL_NAME));
            byte[] icon = cursor.getBlob(cursor.getColumnIndex(Constant.FESTIVAL_ICON));
            String date = cursor.getString(cursor.getColumnIndex(Constant.FESTIVAL_DATE));
            String lunar = cursor.getString(cursor.getColumnIndex(Constant.FESTIVAL_LUNAR));
            String week = cursor.getString(cursor.getColumnIndex(Constant.FESTIVAL_WEEK));
            festival = new Festival(id,name,icon,date,lunar,week);
            list.add(festival);
        }
        cursor.close();
        return list;
    }

    public static List<FestivalMessage> festivalMessageCursorToList(Cursor cursor){
        List<FestivalMessage> list = new ArrayList<>();
        FestivalMessage festivalMessage;
        while (cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex(Constant.FESTIVALMESSAGE_ID));
            int fid = cursor.getInt(cursor.getColumnIndex(Constant.FESTIVALMESSAGE_FESTIVAL_ID));
            String context = cursor.getString(cursor.getColumnIndex(Constant.FESTIVALMESSAGE_CONTENT));
            festivalMessage = new FestivalMessage(id,fid,context);
            list.add(festivalMessage);
        }
        cursor.close();
        return list;
    }


    public static List<WishType> wishTypeCursorToList(Cursor cursor){
        List<WishType> list = new ArrayList<>();
        WishType wishType;
        while (cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex(Constant.WISHTYPE_ID));
            String name = cursor.getString(cursor.getColumnIndex(Constant.WISHTYPE_NAME));
            byte[] icon = cursor.getBlob(cursor.getColumnIndex(Constant.WISHTYPE_ICON));
            wishType = new WishType(id,name,icon);
            list.add(wishType);
        }
        cursor.close();
        return list;
    }

    public static List<WishTypeMessage> wishTypeMessagesCursorToList(Cursor cursor){
        List<WishTypeMessage> list = new ArrayList<>();
        WishTypeMessage wishTypeMessage;
        while (cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex(Constant.WISHTYPEMESSAGE_ID));
            int wid = cursor.getInt(cursor.getColumnIndex(Constant.WISHTYPEMESSAGE_WISHTYPE_ID));
            String context = cursor.getString(cursor.getColumnIndex(Constant.WISHTYPEMESSAGE_CONTENT));
            wishTypeMessage = new WishTypeMessage(id,wid,context);
            list.add(wishTypeMessage);
        }
        cursor.close();
        return list;
    }


    public static List<MyFestival> myFestivalCursorToList(Cursor cursor){
        List<MyFestival> list = new ArrayList<>();
        MyFestival myFestival;
        while (cursor.moveToNext()){
            int id =  cursor.getInt(cursor.getColumnIndex(Constant.MYFESTIVAL_ID));
            String name = cursor.getString(cursor.getColumnIndex(Constant.MYFESTIVAL_NAME));
            String date = cursor.getString(cursor.getColumnIndex(Constant.MYFESTIVAL_DATE));
            String remark = cursor.getString(cursor.getColumnIndex(Constant.MYFESTIVAL_REMARK));

            myFestival = new MyFestival(id,name,date,remark);
            list.add(myFestival);
        }
        cursor.close();
        return list;
    }

}
